package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Orders;
import com.itheima.service.OrdersService;
import com.itheima.service.impl.OrdersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/orders/*")
public class OrdersServlet extends BaseServlet {
    OrdersService orderService = new OrdersServiceImpl();
    private int pageSize=2;
     private  String dd=null;
    //查询所有订单
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        List<Orders> list;
        String pageNum1 = req.getParameter("pageNum");
         String pageSize1 = req.getParameter("pageSize");
          String dd1 = req.getParameter("dd");
        if (pageNum1 != null && pageNum1 != "") {
            pageNum = Integer.parseInt(pageNum1);
        }
        if (pageSize1 != null && pageSize1 != "") {
            pageSize = Integer.parseInt(pageSize1);
        }
        //判断是否是模糊查询
        if (dd1!= null && dd1 != ""){
            dd=dd1;
             list=orderService.findAllByLike(pageNum,pageSize,dd);
        }else {
            list = orderService.findAll(pageNum,pageSize);
        }

        PageInfo<Orders> pageInfo = new PageInfo<Orders>(list);
       // req.setAttribute("");

        req.setAttribute("pageInfo", pageInfo);
        req.setAttribute("pageSize",pageSize);
        req.setAttribute("dd",dd);
        req.getRequestDispatcher("/pages/orders-list.jsp").forward(req, resp);
    }

    //查询单个订单
    protected void findOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Orders orders= orderService.findById(id);
        req.setAttribute("orders",orders);
        req.getRequestDispatcher("/pages/orders-show.jsp").forward(req,resp);
    }
    //批量删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String arr = req.getParameter("arr");
        String[] split = arr.split(",");
        orderService.delete(split);
        req.getRequestDispatcher("/orders/findAll").forward(req,resp);
    }

}
